#include "SlavePanelForm.h"
#include "ui_SlavePanelForm.h"
#include "common/Utils.h"

SlavePanelForm::SlavePanelForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SlavePanelForm),m_DevType(0),faultsState(false),faultsStatePre(false)
{
    ui->setupUi(this);

    m_Thread = new QThread();
    m_ModBusTcpWorker = new ModBusTcpWorker(nullptr);

    // 将 worker 移动到新线程，并确保在线程结束时删除
    m_ModBusTcpWorker->moveToThread(m_Thread);
    connect(m_Thread, &QThread::finished, m_Thread, &QObject::deleteLater);

    // 当 worker 被删除时，结束线程
    connect(m_ModBusTcpWorker, &QObject::destroyed, m_Thread, &QThread::quit);

    // 获取QCoreApplication的实例
    QCoreApplication *app = QCoreApplication::instance();
    if(app)
    {
        // 确保在应用程序退出时线程已经结束
        connect(app, &QCoreApplication::aboutToQuit, m_Thread, &QThread::quit);
        connect(m_Thread, &QThread::finished, app, &QCoreApplication::quit);
    }

    connect(this,&SlavePanelForm::sig_SetProNameDevType,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetProNameDevType,Qt::QueuedConnection);
    connect(this,&SlavePanelForm::sig_SetIPPort,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetIPPort,Qt::QueuedConnection);
    connect(this,&SlavePanelForm::sig_SetReadStartAddressAndRegisterCount,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetReadStartAddressAndRegisterCount,Qt::QueuedConnection);
    connect(this,&SlavePanelForm::sig_StartLoopReadRegister,m_ModBusTcpWorker,&ModBusTcpWorker::slotStartConnection,Qt::QueuedConnection);
    connect(this,&SlavePanelForm::sig_WriteRegister,m_ModBusTcpWorker,&ModBusTcpWorker::slotAddWriteQueue,Qt::QueuedConnection);

    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_ConnectionState,this,&SlavePanelForm::slot_ConnectionState,Qt::QueuedConnection);
    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_readfloatData,this,&SlavePanelForm::slot_ReadfloatData,Qt::QueuedConnection);
    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_readBoolData,this,&SlavePanelForm::slot_ReadBoolData,Qt::QueuedConnection);

    m_Thread->start();


    ui->stackedbtnWidget->setCurrentIndex(0);
    m_Timer = new QTimer(this);
    connect(m_Timer,&QTimer::timeout,this,&SlavePanelForm::timeoutWatchDog);
//    m_Timer->start(500);



}

SlavePanelForm::~SlavePanelForm()
{
    delete ui;
}

void SlavePanelForm::setProNameDevType(QString proName, int devType)
{
    ui->labDevId->setText(QString("设备%1").arg(devType));
    m_DevType = devType;
    emit sig_SetProNameDevType(proName,devType);
}

void SlavePanelForm::setIPPort(QString address, short port)
{
    ui->SlaveIP->setText(address);
    emit sig_SetIPPort(address,port);
}

void SlavePanelForm::setReadStartAddressAndRegisterCount(QList<int> startAddress, QList<int> registerCount, int RegisterType)
{
    emit sig_SetReadStartAddressAndRegisterCount(startAddress,registerCount,RegisterType);
}

void SlavePanelForm::startLoopReadRegister(bool start)
{
    emit sig_StartLoopReadRegister(start);
}

//总控的故障传递过来的
void SlavePanelForm::slot_Devfaults(bool faultBool)
{
    faultsState = faultBool;
    if(faultsState && !faultsStatePre)
    {
        m_Timer->start(700);//开始闪烁灯
    }
    else if(!faultsState && faultsStatePre)
    {
        ui->stackedbtnWidget->setCurrentIndex(0);
        m_Timer->stop();
    }
    faultsStatePre = faultsState;
}

//TCP的连接状态
void SlavePanelForm::slot_ConnectionState(bool State)
{
    if(State)
    {
       ui->SlaveIP->setStyleSheet("color:#4db5f6");
    }
    else
    {
       ui->SlaveIP->setStyleSheet("color:#808080");
       ui->labValue_1->setText("--");
       ui->labValue_2->setText("--");
       ui->labValue_3->setText("--");
       ui->labValue_4->setText("--");
       ui->labValue_5->setText("--");
       ui->labValue_6->setText("--");
       ui->labValue_7->setText("--");
       ui->labValue_8->setText("--");
       ui->labValue_9->setText("--");
       ui->labValue_10->setText("--");
       ui->labValue_11->setText("--");

       ui->btnState_1->setChecked(false);
       ui->btnState_2->setChecked(false);
       ui->btnState_3->setChecked(false);
       ui->btnState_4->setChecked(false);
       ui->btnState_5->setChecked(false);
       ui->btnState_6->setChecked(false);
       ui->btnState_7->setChecked(false);

       ui->btnState_8->setChecked(false);
       ui->btnState_9->setChecked(false);
       ui->btnState_10->setChecked(false);
       ui->btnState_11->setChecked(false);
       ui->btnState_12->setChecked(false);
       ui->btnState_13->setChecked(false);
       ui->btnState_14->setChecked(false);




    }
    ui->btnState->setChecked(State);
}

//最终界面显示的浮点数
void SlavePanelForm::slot_ReadfloatData(int MsgId,QList<float> data)
{
    Q_UNUSED(MsgId)
    if(data.size() != 11)
        return;

    ui->labValue_1->setText(QString::number(data.at(0),'f',1));
    ui->labValue_2->setText(QString::number(data.at(1),'f',1));
    ui->labValue_3->setText(QString::number(data.at(2),'f',1));
    ui->labValue_4->setText(QString::number(data.at(3),'f',1));
    ui->labValue_5->setText(QString::number(data.at(4),'f',1));
    ui->labValue_6->setText(QString::number(data.at(5),'f',1));
    ui->labValue_7->setText(QString::number(data.at(6),'f',1));
    ui->labValue_8->setText(QString::number(data.at(7),'f',1));
    ui->labValue_9->setText(QString::number(data.at(8),'f',1));

    ui->labValue_10->setText(QString::number(data.at(9),'f',1));
    ui->labValue_11->setText(QString::number(data.at(10),'f',1));

}

//最终界面显示的bool
void SlavePanelForm::slot_ReadBoolData(int MsgId,QList<bool> data)
{
    Q_UNUSED(MsgId)
    if(data.size() != 14)
        return;
    /*
    for(int i = 0 ; i < 14; i++)
    {
        QPushButton*  btnPtr = this->findChild<QPushButton*>(QString("btnState_%1").arg(i+1));
        if(btnPtr)
        {
            btnPtr->setChecked(data.at(i));
        }
    }
    */
    ui->btnState_1->setChecked(!data.at(0));
    ui->btnState_2->setChecked(!data.at(1));
    ui->btnState_3->setChecked(!data.at(2));
    ui->btnState_4->setChecked(!data.at(3));
    ui->btnState_5->setChecked(!data.at(4));

    ui->btnState_6->setChecked(!data.at(5));
    ui->btnState_7->setChecked(!data.at(6));
    ui->btnState_8->setChecked(!data.at(7));
    ui->btnState_9->setChecked(!data.at(8));
    ui->btnState_10->setChecked(!data.at(9));

    ui->btnState_11->setChecked(!data.at(10));
    ui->btnState_12->setChecked(!data.at(11));
    ui->btnState_13->setChecked(!data.at(12));
    ui->btnState_14->setChecked(!data.at(13));

}


void SlavePanelForm::on_btnReset_clicked()
{
    quint16  iValue = 0;
    iValue = quint16ModifyBit(iValue,1,true);
    QList<quint16> sendData1;
    sendData1 << iValue;

    WriteData* writeData1 = new WriteData(3,sendData1.size(),sendData1);
    emit sig_WriteRegister(writeData1);
}

void SlavePanelForm::timeoutWatchDog()
{
    if(ui->btnState->isChecked())
    {
        if(faultsState)
        {
            int currIndex = ui->stackedbtnWidget->currentIndex();
            if(currIndex == 0)
                ui->stackedbtnWidget->setCurrentIndex(1);
            else if(currIndex == 1)
                ui->stackedbtnWidget->setCurrentIndex(0);
        }
    }
}
